package com.zkyc.framework.common.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zkyc.framework.common.annotation.SqlAnnotation;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 此类主要面向高级查询
 * @author libingchuan
 */
public class SqlConditionUtil {

    /**
     * 高级查询自动填充条件与值
     * @param tableName 表名 表有别名写别名
     * @param obj 类 值的类
     * @param queryWrapper 需要填充的queryWrapper
     * @return 泛型QueryWrapper
     * @throws Exception 反射异常
     */
    public static QueryWrapper<?> getQueryWrapper(String tableName,Object obj,QueryWrapper<?> queryWrapper) throws Exception {
        Class clz = obj.getClass();
        Field[] fields = clz.getDeclaredFields();
        //fields = delElement(fields,0);
        for(int i=0;i<fields.length;i++){
            Method method = obj.getClass().getMethod("get"+getMethodName(fields[i].getName()));
            Object param = method.invoke(obj);
            Field field = fields[i];
            if(param!=null) {
                switch (fields[i].getGenericType().toString()) {
                    case "class java.lang.String":
                        if(StringUtils.isNotEmpty(param.toString())){
                            if (fields[i].isAnnotationPresent(SqlAnnotation.class)){
                                queryWrapper.like(tableFieldName(tableName,field.getName()),param);
                            }
                            else{
                                queryWrapper.eq(tableFieldName(tableName,field.getName()),param);
                            }
                        }
                        break;
                    case "class java.lang.Integer":
                        queryWrapper.eq(tableFieldName(tableName,field.getName()),param);
                        break;
                    case "int":
                        if(Integer.parseInt(param.toString())!=0){
                            System.out.println(Integer.parseInt(param.toString()));
                        }
                        break;
                    case "class java.math.BigDecimal":
                        queryWrapper.eq(tableFieldName(tableName,field.getName()),param);
                        break;
                    case "class java.time.LocalDateTime":
                        queryWrapper.eq(tableFieldName(tableName,field.getName()),param);
                        break;
                }
            }
        }
        return queryWrapper;
    };


    /**
     * 获取方法名
     * @param fildeName
     * @return
     * @throws Exception
     */
    private static String getMethodName(String fildeName) throws Exception{
        byte[] items = fildeName.getBytes();
        items[0] = (byte) ((char) items[0] - 'a' + 'A');
        return new String(items);
    }

    /**
     * 删除类字段集的不需要数据
     * @param arrays
     * @param index
     * @return
     */
    public static Field[] delElement(Object[] arrays, int index) {
        int length = arrays.length;
        //判断数据合理性
        if (index >= 0 && index < length) {
            Field[] arrays_result = new Field[arrays.length - 1];
            //将arrays数组在index前的元素都复制到新数组arrays_result中
            System.arraycopy(arrays, 0, arrays_result, 0, index);
            //判断index之后是否还有元素，有则将index后的元素从index位置复制到新数组中
            if (index < length - 1) {
                System.arraycopy(arrays, index + 1, arrays_result, index, arrays_result.length - index);
            }
            return arrays_result;
        } else {
            //不合理，抛越界异常
            throw new IndexOutOfBoundsException("index :" + index + ", length: " + length);
        }
    }

    /**
     * 字段名与表名
     * @param tableName
     * @param fieldName
     * @return
     */
    public static String tableFieldName(String tableName,String fieldName){
        StringBuffer tableField = new StringBuffer();
        tableField.append(tableName+".");
        for(int i =0;i<fieldName.length();i++){
            char c = fieldName.charAt(i);
            if(Character.isUpperCase(c)){
                tableField.append('_');
            }
            tableField.append(Character.toLowerCase(c));
        }
        return tableField.toString();
    }
}